package com.cn.jx.manager.service.mtn.Impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.cn.jx.common.service.BaseService;
import com.cn.jx.manager.dao.NoticeRepository;
import com.cn.jx.manager.form.NoticeForm;
import com.cn.jx.manager.po.Notice;
import com.cn.jx.manager.service.mtn.INoticeServiceMtn;
import com.jx.enumType.OperatEnum;
import com.jx.util.ObjectTransUtil;
import com.jx.util.Utility;

@Service("noticeServiceMtn") 
@Transactional
public class NoticeServiceImplMtn extends BaseService implements INoticeServiceMtn {

	@Autowired
	private NoticeRepository noticeDao;
	
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void saveOrUpdateNotice(NoticeForm item) throws Exception {
		if (Utility.isNotBlank(item.getId())) {
			Notice notice = findEntityByPkid(item.getId(), Notice.class);// 得到库中的数据
			Notice uo = ObjectTransUtil.getUpdateObj(notice, item);
			if (uo != null) {
				uo.setOperatTime(new Date());
				uo.setOperatUser(item.getOperatUser());
				Notice notiss = noticeDao.save(uo);
				logUtil.LogMsg(Notice.class, notiss.getId(), OperatEnum.UPDATE, item.getOperatUser());
			}
		} else {
			Notice not = Notice.getInstance(item.getOperatUser());
			not.setTitle(item.getTitle());
			not.setSubTitle(item.getSubTitle());
			not.setContent(item.getContent());
			Notice nots = noticeDao.save(not);
			logUtil.LogMsg(Notice.class, nots.getId(), OperatEnum.SAVE, item.getOperatUser());
		}
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void del(List<String> ids, String opter) throws Exception {
		deleteEntityByIds(ids, Notice.class);
		logUtil.LogMsg(Notice.class, Utility.getListToStr(ids), OperatEnum.DELETE, opter);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void remove(List<String> ids, String opertor) throws Exception {
		removeEntityByIds(ids, Notice.class);
		logUtil.LogMsg(Notice.class, Utility.getListToStr(ids), OperatEnum.REMOVE, opertor);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void recover(List<String> ids, String opertor) throws Exception {
		recoverEntityById(ids, Notice.class);
		logUtil.LogMsg(Notice.class, Utility.getListToStr(ids), OperatEnum.RECOVER, opertor);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void removeAll(String opertor) throws Exception {
		removeAllEntity(Notice.class);
		// 保存日志
		logUtil.LogMsg(Notice.class, "ALL", OperatEnum.REMOVE, opertor);
	}

}
